package com.soul.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soul.common.bean.vo.DictionaryVo;
import com.soul.common.exception.BaseException;
import com.soul.common.utils.ListUtil;
import com.soul.sys.bean.dto.dict.DictAddDto;
import com.soul.sys.bean.dto.dict.DictSearchDto;
import com.soul.sys.bean.dto.dict.DictUpdateDto;
import com.soul.sys.bean.po.DictPo;
import com.soul.sys.bean.vo.DictVo;
import com.soul.sys.mapper.DictMapper;
import com.soul.sys.service.DictService;
import com.soul.common.service.BaseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author sws
 * @since 2023-09-27 10:08:12
 */
@Service
public class DictServiceImpl extends BaseService<DictMapper, DictPo> implements DictService {

    /**
     * 根据字典代码查询字典数据
     * @param typeCode
     * @return
     */
    @Override
    public List<DictPo> selectByTypeCode(String typeCode){
        return this.getLambadaQueryWrapper().eq(DictPo::getTypeCode, typeCode).list();
    }

    /**
     * 分页查询数据字典类型
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public IPage<DictVo> queryDictList(Integer currentPage, Integer size, DictSearchDto searchDto){
        // 分页
        IPage<DictPo> iPage = new Page<>(currentPage,size);
        // 查询数据字典类型列表
        // 查询条件
        LambdaQueryWrapper<DictPo> queryWrapper = Wrappers.lambdaQuery(DictPo.class);
        if (ObjectUtil.isNotEmpty(searchDto)){
            queryWrapper.eq(ObjectUtil.isNotEmpty(searchDto.getTypeCode()),DictPo::getTypeCode,searchDto.getTypeCode())
                    .like(ObjectUtil.isNotEmpty(searchDto.getDictCode()),DictPo::getDictCode,searchDto.getDictCode())
                    .like(ObjectUtil.isNotEmpty(searchDto.getDictName()),DictPo::getDictName,searchDto.getDictName());
        }
        IPage<DictPo> typePage = this.page(iPage,queryWrapper);
        return typePage.convert(this::toDataVo);
    }

    @Override
    public List<DictVo> queryDictByTypeCode(String typeCode) {
        if (ObjectUtil.isEmpty(typeCode)){
            return new ArrayList<>();
        }
        List<DictPo> list = this.getLambadaQueryWrapper().eq(DictPo::getTypeCode, typeCode).list();
        return ListUtil.toValueList(list,this::toDataVo);
    }

    @Override
    public DictVo getDictInfo(Long id){
        DictPo typePo = this.getById(id);
        return this.toDataVo(typePo);
    }

    @Override
    public void addDict(DictAddDto addDto){
        // 校验代码重复
        this.verifyCode(addDto.getTypeCode());
        DictPo DictPo = BeanUtil.copyProperties(addDto, DictPo.class);
        // 排序号
        Long maxSort = this.getMaxSort();
        DictPo.setSort(maxSort);
        this.save(DictPo);
    }

    @Override
    public void updateDict(DictUpdateDto updateDto){
        // 判断是否存在
        DictPo typePo = this.getById(updateDto.getId());
        if (ObjectUtil.isEmpty(typePo)){
            throw new BaseException("数据已不存在");
        }
        // 校验代码是否重复
        this.verifyCode(updateDto.getDictCode(),updateDto.getId());
        BeanUtil.copyProperties(updateDto,typePo);
        this.updateById(typePo);
    }

    /**
     * 删除字典
     * @param id
     */
    @Override
    public void deleteDict(Long id){
        DictPo typePo = this.getById(id);
        if (ObjectUtil.isEmpty(typePo)){
            throw new BaseException("数据不存在");
        }
        this.removeById(id);
    }
    private void verifyCode(String code,Long id){
        List<DictPo> typePos = this.getLambadaQueryWrapper().eq(DictPo::getDictCode, code).list();
        if (ObjectUtil.isNotEmpty(typePos)){
            if (ObjectUtil.isEmpty(id)){
                throw new BaseException("代码不能重复");
            }
            List<DictPo> collect = typePos.stream().filter(p -> !Objects.equals(p.getId(), id)).collect(Collectors.toList());
            if (ObjectUtil.isNotEmpty(collect)){
                throw new BaseException("代码不能重复");
            }
        }
    }
    private void verifyCode(String code){
        this.verifyCode(code,null);
    }
    private DictVo toDataVo(DictPo po){
        return BeanUtil.copyProperties(po, DictVo.class);
    }

    @Override
    public List<DictionaryVo> queryDictList(Collection<String> dictTypes){
        List<DictPo> list = this.getLambadaQueryWrapper().in(DictPo::getTypeCode, dictTypes).list();
        return ListUtil.toValueList(list,t->{
            DictionaryVo dictionaryVo = new DictionaryVo();
            dictionaryVo.setId(t.getDictCode()).setType(t.getTypeCode()).setName(t.getDictName());
            return dictionaryVo;
        });
    }
}
